﻿using System;
using System.IO;
using UnityEngine;
using QGMiniGame;
using System.Collections.Generic;

public class GGFileIOQG : GGFileIO
{
    Dictionary<string, string> textDic = new Dictionary<string, string>();
    Dictionary<string, byte[]> bytesDic = new Dictionary<string, byte[]>();
    //byte[] bytes = null;
    //string text = null;
    private string GetInternalPath()
    {
        return "internal://files";
    }
    protected string FullPath(string filename)
    {
        return GetInternalPath() + "/" + filename;
    }
    protected string RevertFilename(string path)
    {
        string[] parts = path.Split('/');
        string fileName = parts[parts.Length - 1]; // 使用索引从尾部获取元素  
        Debug.LogError(fileName);
        return fileName;
    }
    public override void Write(string path, string text)
    {
        PlayerPrefs.SetString(path, text);
        PlayerPrefs.Save();
    }
    public override void Write(string path, byte[] bytes)
    {
        string base64String = System.Convert.ToBase64String(bytes);
        // 使用PlayerPrefs存储字符串
        PlayerPrefs.SetString(path, base64String);
        PlayerPrefs.Save();
    }
    public override string ReadText(string path)
    {
        string base64String = PlayerPrefs.GetString(path, "");
        return base64String;
    }

    public override byte[] Read(string path)
    {
        string base64String = PlayerPrefs.GetString(path, "");
        if (!string.IsNullOrEmpty(base64String))
        {
            return System.Convert.FromBase64String(base64String);
        }
        return null;
    }

    public override bool FileExists(string path)
    {
        if (PlayerPrefs.HasKey(path))
            return true;
        else
            return false;
    }

    public override DateTime LastWriteTimeUTC(string path)
    {
        return DateTime.UtcNow;
    }

    public override Stream FileReadStream(string path)
    {
        byte[] array = Read(path);
        return new MemoryStream(array, 0, array.Length);
    }
}
